package com.lj.luoye.lotteryticket.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.lj.luoye.lotteryticket.entity.Register;

import java.util.ArrayList;
import java.util.List;

public class RegisterDao {
    public final static String table_name = "register_history";
    public DBHelper dbHelper;
    public Context context;
    String[] fields = new String[]{"id","date","startDate","tryDays","tryCount", "code"};

    public RegisterDao(Context context) {
        this.dbHelper = new DBHelper(context);
        this.context = context;
    }

    public Register getOne(){
        List<Register> all = getAll();
        if(all.size()>0){
            return all.get(0);
        }
        return null;
    }

    public List<Register> getAll(){
        List<Register> registerList = new ArrayList<>();
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        Cursor cursor = db.query(RegisterDao.table_name,fields,null,null,null,null,null);
        while(cursor.moveToNext()){
            int id = cursor.getInt(cursor.getColumnIndex("id"));
            String date = cursor.getString(cursor.getColumnIndex("date"));
            String startDate = cursor.getString(cursor.getColumnIndex("startDate"));
            int tryDays= cursor.getInt(cursor.getColumnIndex("tryDays"));
            int tryCount= cursor.getInt(cursor.getColumnIndex("tryCount"));
            String code = cursor.getString(cursor.getColumnIndex("code"));
            Register register = new Register(id,date,startDate,code);
            register.tryDays=tryDays;
            register.tryCount=tryCount;
            registerList.add(register);
        }
        db.close();
        return registerList;
    }

    public int insert(Register register){
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put("date",register.date);
        contentValues.put("startDate",register.startDate);
        contentValues.put("tryDays",register.tryDays);
        contentValues.put("tryCount",register.tryCount);
        contentValues.put("code",register.code);
        long id = db.insert(RegisterDao.table_name, null, contentValues);
        db.close();
        return (int) id;
    }

    public int updateCode(int id,String code){
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put("code",code);
        int res = db.update(RegisterDao.table_name, contentValues, "id=?", new String[]{id+""});
        db.close();
        return res;
    }

    public int updateStart(int id,String startDate,int tryDays,int tryCount){
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put("startDate",startDate);
        contentValues.put("tryDays",tryDays+"");
        contentValues.put("tryCount",tryCount+"");
        int res = db.update(RegisterDao.table_name, contentValues, "id=?", new String[]{id+""});
        db.close();
        return res;
    }

    public int clean(){
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        int count=db.delete(RegisterDao.table_name,null,null);
        db.close();
        return count;
    }

}
